<template>
    <div class="ShowFile">
        <div>
            <img v-if="isImages" :src="`${fileHost + apiPath}/file/${file.file_sign}`" @contextmenu="setShowMaxImg" />
            <div v-else>
                <div><i class="fa fa-file-o"></i></div>
                <div>{{ file.file_ext }}</div>
            </div>
        </div>
        <div v-if="showTitle">
            <div class="files-list-item-name">{{ file.file_name }}</div>
        </div>
        <div v-if="showDownload" class="ShowFile-download" @click.stop>
            <div><i class="el-icon-download"></i></div>
            <div><a target="_blank" :href="getDownloadUrl">下载到本地</a></div>
        </div>
    </div>
    <transition name="scale-origin-animation">
        <div class="ShowFile-max" v-if="showMaxImg" @contextmenu="showMaxImg = false" @click.stop="showMaxImg = false">
            <img ref="seeMaxImg" :src="`${fileHost}/api/file/${file.file_sign}`" @contextmenu="showMaxImg = false" @click.stop="showMaxImg = false" />
        </div>
    </transition>
</template>

<script lang="ts">
import { computed, defineComponent, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import config from "@/config";
import utils from "@/lib/utils";
import setCssVal from "@/lib/setCssVal";
/**
 * state 数据类型
 */
interface IState {
    /**
     *  是否显示大图
     */
    showMaxImg: boolean;
}
export default defineComponent({
    name: "ShowFile",
    props: {
        file: {
            type: Object,
            default: () => {
                return {
                    file_sign: "",
                    file_name: "",
                    file_ext: "",
                };
            },
        },
        showTitle: {
            type: Boolean,
            default: true
        },
        showDownload: {
            type: Boolean,
            default: true
        }
    },
    setup(props) {
        const store = useStore();
        const { UIAuth, bodySize } = toRefs(store.state);
        /**
         * 文件服务器
         */
        const fileHost: string = config.fileHost;
        /**
         * api 根路径
         */
        const apiPath: string = config.apiPath;
        /**
         * 图片文件后缀名
         */
        const imageExt: string[] = config.imageFileExt;
        const state = reactive<IState>({
            showMaxImg: false,
        });

        const setShowMaxImg = (event: MouseEvent) => {
            setCssVal.setCssVals({
                "--p-origin-x": event.clientX + "px",
                "--p-origin-y": event.clientY + "px",
            });
            state.showMaxImg = true;
        };
        return {
            UIAuth,
            bodySize,
            ...toRefs(state),
            fileHost,
            apiPath,
            imageExt,
            setShowMaxImg,
            isImages: computed((): boolean => {
                {
                    if (utils.empty(props.file.file_ext)) {
                        return false;
                    }
                    const index = imageExt.findIndex(
                        (i: string) => i === props.file.file_ext
                    );
                    return index >= 0;
                }
            }),
            getDownloadUrl: computed((): string => {
                return fileHost + apiPath + "/download/" + props.file.file_sign;
            }),
        };
    },
});
</script>


<style scoped lang="scss">
.ShowFile {
    overflow: hidden;
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 5px;
    box-shadow: 0 0 5px #c0c0c0;
    & > div {
        &:nth-child(1) {
            overflow: hidden;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f8f8f8;
            cursor: pointer;
            & > img {
                max-width: 100%;
                max-height: 100%;
                transition: all 0.3s ease-in;
                &:hover {
                    max-width: 150%;
                    max-height: 150%;
                }
            }
            & > div {
                transition: all 0.3s ease-in;
                text-align: center;
                & > div {
                    font-weight: bold;
                    font-size: 18px;
                    color: #da7803;
                    &:nth-child(1) {
                        font-size: 50px;
                        padding-bottom: 5px;
                    }
                }
            }
            &:hover {
                & > div {
                    transform: scale(1.8);
                }
            }
        }
        &:nth-child(2) {
            position: absolute;
            bottom: 0;
            left: 0;
            height: 25px;
            line-height: 25px;
            padding: 0 10px;
            color: #ffffffff;
            cursor: pointer;
            width: calc(100% - 20px);
            background-color: rgba($color: #000000, $alpha: 0.3);
            transition: all 0.2s ease-in;
            word-wrap: break-word;
            &:hover {
                height: calc(100% - 25px);
                background-color: rgba($color: #000000, $alpha: 0.6);
            }
        }
    }
    &:hover {
        & .ShowFile-download {
            top: 0;
        }
    }
    &-download {
        position: absolute;
        top: -26px;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 25px;
        background-color: rgba($color: #000000, $alpha: 0.3);
        transition: all 0.2s ease-in;
        cursor: pointer;
        & > div {
            margin: 0 2px;
            color: #ffffff;
            & > a {
                color: #ffffff;
                text-decoration: none;
                &:hover {
                    color: #00fffd;
                }
            }
        }
        &:hover {
            background-color: rgba($color: #000000, $alpha: 0.6);
        }
    }
    &-name {
        height: 100%;
        line-height: 25px;
        &:hover {
            color: #f1f1f1;
            height: 100%;
        }
    }
    // 预览大图
    &-max {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background-color: rgba($color: #000000, $alpha: 0);
        display: flex;
        align-items: center;
        justify-content: center;
        & > img {
            position: absolute;
            max-width: 85%;
            max-height: 90%;
            box-shadow: 0 0 30px #000000;
        }
    }
}
</style>